<?php
namespace App\Http\Controllers\Admin\Order;

use App\Repositories\Admin\CategoryRepository;
use App\Repositories\Admin\OrderBaleRepository;
use App\Repositories\Admin\CompanyRepository;
use App\Services\Admin\OrderBaleService;
use App\Utilities\JiugeForm;
use App\Utilities\Upload;
use Illuminate\Http\Request;

class BaleController extends BaseController
{
    /**
     * 货单
     */

    protected $view = 'admin.order.bale.';//视图前缀
    protected $url = 'bale';//路由前缀
    protected $model;
    protected $com;
    protected $category;
//    protected $formElementArr = [//表单元素
//        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
//        array(5,'发布的企业','cid','发布的企业，必选',6),
//        array(1,'产品名称','name','产品名称，必填',1),
//        array(5,'搜索分类','cate','搜索分类，必选',6),
//        array(1,'款号','styleNo','款号，必填',1),
//        array(1,'款式名称','styleName','款式名称，必填',1),
//        array(1,'数量','num','数量，必填',5),
//        array(1,'单价','money','单价，必填',4),
//        array(1,'适用场景','scence','适用场景',100),
//        array(1,'材质','material','材质',100),
//        array(1,'工艺要求','tech','工艺要求',100),
//        array(5,'外发要求','proType','外发要求，必选',6),
//        array(5,'外发范围','region','外发范围，必选',6),
//        array(5,'生产要求','proWay','生产要求，必选',6),
//        array(1,'有效期','period','有效期，必选',4),
//    ];

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new OrderBaleRepository();
        $this->com = new CompanyRepository();
        $this->category = new CategoryRepository();
        view()->share('prefix_url',$this->prefix_url);
    }

    public function index()
    {
        $models = $this->model->getAdminIndex();
        return view($this->view.'index',array(
            'datas' => $models,
        ));
    }

    /**
     * 基本信息编辑
     */
    public function edit(Request $request)
    {
        $id = isset($request->id) ? $request->id : 0;
        $model = $this->model->getAdminShow($id);
        $coms = $this->com->getAdminCompanys();
        $cates = $this->category->getCatesByPid(167);
        $outTypes = $this->model->getCatesByPid(174);
        return view($this->view.'edit',array(
            'coms' => $coms,
            'cates' => $cates,
            'outTypes' => $outTypes,
            'data' => $model,
        ));
    }

    /**
     * 通过企业类型，获取外发分类
     */
    public function getOutTypes(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $cate = $request->cate;
        if ($cate==1) {
            $dataArr = $this->model->getCatesByPid(174);
        } else {
            $dataArr = $this->model->getCatesByPid(185);
        }
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$dataArr);
    }

    /**
     * 基本信息设置
     */
    public function setBasic(Request $request)
    {
        $res = $request->all();
        $fields = array(
            'uid','name','cate','styleNo','styleName','num','money','period',
        );
        $field2s = array(
            'outTypes','isRob','isFabric','isContract',
        );
        foreach ($fields as $field) {
            if (!$res[$field]) {
                echo "<script>";
                echo "alert('数据不全！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
        }
        foreach ($field2s as $field2) {
            if (!isset($res[$field2])) {
                echo "<script>";
                echo "alert('数据不全2！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
        }
        $dataArr = array(
            'uid' => $res['uid'],
            'name' => $res['name'],
            'cate' => $res['cate'],
            'styleNo' => $res['styleNo'],
            'styleName' => $res['styleName'],
            'num' => $res['num'],
            'money' => $res['money'],
            'period' => strtotime($res['period']),
            'outTypeStr' => implode(',',$res['outTypes']),
        );
        $model = $this->model->getShowOne($res['id']);
        if (!$model) {
            $dataArr['scence'] = $res['scence'] ? $res['scence'] : '';
            $dataArr['material'] = $res['material'] ? $res['material'] : '';
            $dataArr['tech'] = $res['tech'] ? $res['tech'] : '';
            $dataArr['detail'] = $res['detail'] ? $res['detail'] : '';
            $lastId = $this->model->setAdd($dataArr);
        } else {
            $this->model->setModify($dataArr,$model->id);
            $lastId = $model->id;
        }
        return redirect($this->prefix_url.'/other?id='.$lastId);
    }

    /**
     * 设置删除
     */
    public function setDel(Request $request)
    {
        $id = $request->id;
        $this->model->setDel($id);
        return redirect($this->prefix_url);
    }

    /**
     * 其他信息编辑
     */
    public function editOther(Request $request)
    {
        $id = $request->id;
        $model = $this->model->getAdminShow($id);
        //尺寸颜色
        $sizes = $model->sizelist ?
            $this->model->objToArr($model->sizelist) : array();
        $colors = $this->model->getSizelistColors();
        //主图、细节图集
        $imgs[0] = $model->img ? $model->img : '';
        $resImgs = $model->imgDetails ?
            $this->model->objToArr($model->imgDetails) : array();
        $imgs[1] = $resImgs ? $resImgs[0] : '';
        $imgs[2] = $resImgs ? $resImgs[1] : '';
        $imgs[3] = $resImgs ? $resImgs[2] : '';
        $imgs[4] = $resImgs ? $resImgs[3] : '';
        $imgs[5] = $resImgs ? $resImgs[4] : '';
        //面辅料成分/缩率
        $stuffs = $model->stuff ?
            $this->model->objToArr($model->stuff) : array();
        //面辅料单耗
        $depletes = $model->deplete ?
            $this->model->objToArr($model->deplete) : array();
        //产品类型
        $orderTypes = $model->orderType ?
            $this->model->objToArr($model->orderType) : array();
        //产品风格
        $styles = array();
        if ($styleStr=$model->style) {
            $styles = explode('||',$styleStr);
//            $resStyles = explode('||',$styleStr);
//            foreach ($resStyles as $key => $resStyle) {
//                $resCate = $this->model->getCateNameById($resStyle);
//                $styles[$key]['id'] = $resStyle;
//                $styles[$key]['name'] = $resCate;
//            }
        }
        //产品款式
        $designs = array();
        if ($designStr=$model->design) {
            $designs = explode('||',$designStr);
//            $resDesigns = explode('||',$designStr);
//            foreach ($resDesigns as $key => $resDesign) {
//                $resCate = $this->model->getCateNameById($resDesign);
//                $designs[$key]['id'] = $resDesign;
//                $designs[$key]['name'] = $resCate;
//            }
        }
//        dd($sizes,$colors,$imgs,$stuffs,$depletes,$orderTypes,$styles,$designs);
        return view($this->view.'other',array(
            'colors' => $colors,
            'sizes' => $sizes,
            'imgs' => $imgs,
            'stuffs' => $stuffs,
            'depletes' => $depletes,
            'orderTypes' => $orderTypes,
            'styles' => $styles,
            'designs' => $designs,
            'data' => $model,
        ));
    }

    /**
     * 其他信息，尺寸颜色编辑
     */
    public function getOtherSizes(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $id = $request->id;
        $model = $this->model->getAdminShow($id);
        $colors = $this->model->getSizelistColors();
        $dataArr = array();
        if ($model && $model->sizelist) {
            $dataArr['sizelists'] = $this->model->objToArr($model->sizelist);
        }
        $dataArr['colors'] = $colors;
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$dataArr);
    }

    /**
     * 其他信息，尺寸颜色设置
     */
    public function setOtherSizes(Request $request)
    {
        $res = $request->all();
        $id = $res['id'];
//        $sizes = $this->model->getSizelistSizes();
        $colors = $this->model->getSizelistColors();
//        dd($res,$colors,$res['color0']);
        //白、红、蓝
        $sizelists = array();
        foreach ($colors as $key => $color) {
            if (!isset($res['color'.$key])) {
                echo "<script>";
                echo "alert('数据不全！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $colorId = $res['color'.$key];
            $size0 = isset($res['size'.$key.'-0']) ? 1 : 0;
            $size1 = isset($res['size'.$key.'-1']) ? 1 : 0;
            $size2 = isset($res['size'.$key.'-2']) ? 1 : 0;
            $size3 = isset($res['size'.$key.'-3']) ? 1 : 0;
            $size4 = isset($res['size'.$key.'-4']) ? 1 : 0;
            $size5 = isset($res['size'.$key.'-5']) ? 1 : 0;
            $sizelists[$key] = array(
                $colorId, $size0, $size1, $size2, $size3, $size4, $size5,
            );
        }
        if (!$sizelists) {
            echo "<script>";
            echo "alert('没有数据！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr = array(
            'sizelist' => json_encode($sizelists),
        );
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#size');
    }

    /**
     * 其他信息，图片设置
     */
    public function setOtherImg(Request $request)
    {
        $id = $request->id;
        $key = $request->key;
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        if (!isset($request->imgUrl)) {
            echo "<script>";
            echo "alert('没有上传图片！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr = array();
        if ($key==1) { //主图
            if ($model->img) {
                $imgOld = $model->img;
            }
            $resImg = Upload::uploadImg($request,'imgUrl');
            if ($resImg['code']!=0) {
                echo "<script>";
                echo "alert('上传错误！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $dataArr['img'] = $resImg['filePath'];
        } else if ($key>=2) { //细节图
            $kk = $key - 2;
            if ($imgDetail=$model->imgDetai) {
                $imgDetails = $this->model->objToArr($imgDetail);
                $imgOld = $imgDetails[$kk];
            } else {
                $imgDetails = array('','','','','',);
            }
            $resImg = Upload::uploadImg($request,'imgUrl');
            if ($resImg['code']!=0) {
                echo "<script>";
                echo "alert('上传错误！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $imgDetails[$kk] = $resImg['filePath'];
            $dataArr['imgDetails'] = json_encode($imgDetails);
        } else {
            echo "<script>";
            echo "alert('未知错误！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        if (isset($imgOld) && file_exists(ltrim($imgOld,'/'))) {
            unlink(ltrim($imgOld,'/'));
        }
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#img');
    }

    /**
     * 其他信息，成分缩率编辑
     */
    public function getOtherStuffs(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $id = $request->id;
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr = $model->stuff ?
            $this->model->objToArr($model->stuff) : array();
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$dataArr);
    }

    /**
     * 其他信息，成分缩率设置
     */
    public function setOtherStuffs(Request $request)
    {
        $res = $request->all();
        $id = $res['id'];
        $stuffs = array();
        //老数据
        $stuffCount = $res['stuffCount'];
        for ($i=0;$i<$stuffCount;$i++) {
            $key = $res['stuffKey'.$i];
            $val = $res['stuffVal'.$i];
            if (!$key || !$val) {
                echo "<script>";
                echo "alert('数据".$i."不全！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $stuffs[$i]['key'] = $key;
            $stuffs[$i]['val'] = $val;
        }
        //新增的数据
        $addKey = $res['stuffKey'];
        $addVal = $res['stuffVal'];
        if ($addKey && $addVal) {
            $stuffs[] = array(
                'key' => $addKey,
                'val' => $addVal,
            );
        }
        if (!$stuffs) {
            echo "<script>";
            echo "alert('没有数据！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr['stuff'] = json_encode($stuffs);
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#stuff');
    }

    /**
     * 其他信息，面辅料单耗编辑
     */
    public function getOtherDepletes(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $id = $request->id;
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr = $model->deplete ?
            $this->model->objToArr($model->deplete) : array();
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$dataArr);
    }

    /**
     * 其他信息，面辅料单耗设置
     */
    public function setOtherDepletes(Request $request)
    {
        $res = $request->all();
        $id = $res['id'];
        $depletes = array();
        //老数据
        $depleteCount = $res['depleteCount'];
        for ($i=0;$i<$depleteCount;$i++) {
            $name = $res['depleteName'.$i];
            $size = $res['depleteSize'.$i];
            $num = $res['depleteNum'.$i];
            $remark = $res['depleteRemark'.$i];
            if (!$name || !$size || !$num || !$remark) {
                echo "<script>";
                echo "alert('数据".$i."不全！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $depletes[$i][] = array(
                'name' => $name,
                'size' => $size,
                'num' => $num,
                'remark' => $remark,
            );
        }
        //新增的数据
        $addName = $res['depleteName'];
        $addSize = $res['depleteSize'];
        $addNum = $res['depleteNum'];
        $addRemark = $res['depleteRemark'];
        if ($addName && $addSize && $addNum && $addRemark) {
            $depletes[] = array(
                'name' => $addName,
                'size' => $addSize,
                'num' => $addNum,
                'remark' => $addRemark,
            );
        }
        if (!$depletes) {
            echo "<script>";
            echo "alert('没有数据！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr['deplete'] = json_encode($depletes);
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#deplete');
    }

    /**
     * 其他信息，产品种类编辑
     */
    public function getOtherOrderTypes(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $id = $request->id;
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr = $model->orderType ?
            $this->model->objToArr($model->orderType) : array();
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$dataArr);
    }

    /**
     * 其他信息，产品种类设置
     */
    public function setOtherOrderTypes(Request $request)
    {
        $res = $request->all();
        $id = $res['id'];
        $orderTypes = array();
        //老数据
        $orderTypeCount = $res['orderTypeCount'];
        for ($i=0;$i<$orderTypeCount;$i++) {
            $name = $res['orderTypeName'.$i];
            $male = isset($res['orderTypeMale'.$i]) ? 1 : 0;
            $female = isset($res['orderTypeFemale'.$i]) ? 1 : 0;
            $child = isset($res['orderTypeChild'.$i]) ? 1 : 0;
            if (!$name) {
                echo "<script>";
                echo "alert('数据".$i."不全！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $orderTypes[$i][] = array(
                'name' => $name,
                'male' => $male,
                'female' => $female,
                'child' => $child,
            );
        }
        //新增的数据
        $addName = $res['orderTypeName'];
        $addMale = isset($res['orderTypeMale']) ? 1 : 0;
        $addFemale = isset($res['orderTypeFemale']) ? 1 : 0;
        $addChild = isset($res['orderTypeChild']) ? 1 : 0;
        if ($addName) {
            $orderTypes[] = array(
                'name' => $addName,
                'male' => $addMale,
                'female' => $addFemale,
                'child' => $addChild,
            );
        }
        if (!$orderTypes) {
            echo "<script>";
            echo "alert('没有数据！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $dataArr['orderType'] = json_encode($orderTypes);
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#orderType');
    }

    /**
     * 其他信息，产品风格设置
     */
    public function setOtherStyles(Request $request)
    {
        $id = $request->id;
        $oper = $request->styleOper;
        $key = $request->styleKey;
        $styleName = $request->styleName;
        if (!$id || !$styleName) {
            echo "<script>";
            echo "alert('数据不全！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $styles = array();
        if ($oper=='add') {
            if ($styleStr=$model->style) {
                $styles = explode('||',$model->style);
                $styles[$key] = $styleName;
            } else {
                $styles[] = $styleName;
            }
        } else if ($oper=='modify') {
            if ($key==null) {
                echo "<script>";
                echo "alert('老记录2错误！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $styles = explode('||',$model->style);
            $styles[$key] = $styleName;
        }
        $dataArr = array(
            'style' => implode('||',$styles),
        );
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#style');
    }

    /**
     * 其他信息，产品款式设置
     */
    public function setOtherDesigns(Request $request)
    {
        $id = $request->id;
        $oper = $request->designOper;
        $key = $request->designKey;
        $designName = $request->designName;
        if (!$id || !$designName) {
            echo "<script>";
            echo "alert('数据不全！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $designs = array();
        if ($oper=='add') {
            if ($designStr=$model->design) {
                $designs = explode('||',$model->design);
                $designs[$key] = $designName;
            } else {
                $designs[] = $designName;
            }
        } else if ($oper=='modify') {
            if ($key==null) {
                echo "<script>";
                echo "alert('老记录2错误！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $designs = explode('||',$model->design);
            $designs[$key] = $designName;
        }
        $dataArr = array(
            'design' => implode('||',$designs),
        );
        $this->model->setAdminById($id,$dataArr);
        return redirect($this->prefix_url.'/other?id='.$id.'#design');
    }
}